﻿using System.Data.SQLite;
using System;

namespace DVDTek_Data_Object
{
    internal class Common
    {
        /// <summary>
        /// Replaces all single quotes in string with double quotes
        /// in order to avoird SQL syntax errors
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        internal static string QuoteEscape(string buf)
        {
            if (string.IsNullOrEmpty(buf))
                return buf;
            return buf.Replace("'", "''");
        }

        internal static int BoolToInt(bool val)
        {
            return val.CompareTo(false);
        }

        internal static string ValueOrDBNull(int val)
        {
            if (val >= 0)
                return val.ToString();
            return DBNull.Value.ToString();
        }

        #region QueryExecutionMethods
        /// <summary>
        /// Execute une requete ne renvoyant pas de resultat.
        /// Ex : CREATE, INSERT,....
        /// </summary>
        /// <param name="query"></param>
        internal static void ExecuteNonQuery(string query)
        {
            Accessor.Accessor.Instance.ExecuteNonQuery(query);
        }

        /// <summary>
        /// Execute une requete qui renvoie un object unique.
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        internal static object ExecuteScalarQuery(string query)
        {
            return Accessor.Accessor.Instance.ExecuteScalarQuery(query);
        }

        /// <summary>
        /// Execute une requete et renvoie un reader permettant de lire les résultats.
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        internal static SQLiteDataReader ExecuteQuery(string query)
        {
            return Accessor.Accessor.Instance.ExecuteQuery(query);
        }
        #endregion
    }
}
